//
//  BYVisitorView.m
//  School+
//
//  Created by Sean Sebastian on 4/17/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "BYVisitorView.h"

@implementation BYVisitorView

// override initialization method
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // add subviews by using method
        [self prepareUI];
        
    }
    return self;
}


- (void)prepareUI
{
    // setup the background color
    self.backgroundColor = [UIColor colorWithHue:0 saturation:0 brightness:0.93 alpha:1];
    
    [self addSubview:self.iconView];
    
    // add the cover View in order !
    [self addSubview:self.coverView];
    
    [self addSubview:self.homeView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    
    // remove the influence form autoresizing
    self.iconView.translatesAutoresizingMaskIntoConstraints = false;
    self.homeView.translatesAutoresizingMaskIntoConstraints = false;
    self.messageLabel.translatesAutoresizingMaskIntoConstraints = false;
    self.registerButton.translatesAutoresizingMaskIntoConstraints = false;
    self.loginButton.translatesAutoresizingMaskIntoConstraints = false;
    
    self.coverView.translatesAutoresizingMaskIntoConstraints = false;
    
    // iconView
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.iconView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:-40]];
    
    //  homeView
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];

    // messageLabel
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.iconView attribute:NSLayoutAttributeBottom multiplier:1 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:224]];

    // registerButton
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeBottom multiplier:1 constant:16]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:35]];

    // loginButton
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.registerButton attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:35]];
    

    // coverView
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.coverView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.coverView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.coverView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.registerButton attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];
}

- (void)setupInfo:(NSString *)imageName andMessage:(NSString *)message
{
    self.iconView.image = [UIImage imageNamed:imageName];
    self.messageLabel.text = message;
    [self.messageLabel sizeToFit];
    
    // hidden the horse
    self.homeView.hidden = true;
    
    [self sendSubviewToBack:self.coverView];
}

- (void)startRotationAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.toValue = [NSNumber numberWithDouble:2 * M_PI];
    animation.duration = 20 ;
    animation.repeatCount = MAXFLOAT;
    
    animation.removedOnCompletion = false;
    
    [self.iconView.layer addAnimation:animation forKey:@"homeRotation"];
}

/*

- (void)pauseAnimation
{
    // 记录暂停时间
    CFTimeInterval pauseTime = [self.iconView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    // 设置动画速度为0
    self.iconView.layer.speed = 0;
    // 设置动画偏移时间
    self.iconView.layer.timeOffset = pauseTime;
}

- (void)resumeAnimation
{
    // 获取暂停时间
    CFTimeInterval pauseTime = self.iconView.layer.timeOffset;
    
    // 设置动画速度为1
    self.iconView.layer.speed = 1 ;
    self.iconView.layer.timeOffset = 0 ;
    self.iconView.layer.beginTime = 0 ;
    
    CFTimeInterval timeSincePause = [self.iconView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pauseTime;
    
    self.iconView.layer.beginTime = timeSincePause;
}
 
*/

// buttonClick Method
- (void)registClick
{
    [self.vistorViewDelegate vistorViewRegistClick];
}

- (void)loginClick
{
    [self.vistorViewDelegate vistorViewLoginClick];
}

#pragma mark - lazy loading

- (UIImageView *)iconView
{
    if (_iconView == nil) {
        UIImage *image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        
        _iconView  = imageView;
        return _iconView;
    }
    return _iconView;
}

- (UIImageView *)homeView
{
    if (_homeView == nil) {
        UIImage *image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        _homeView  = imageView;
        
        return _homeView;
    }
    return _homeView;
}

- (UILabel *)messageLabel
{
    if (_messageLabel == nil) {
        UILabel *label = [[UILabel alloc] init];
        label.text = @"这是一条超级超级超级超级超级超级超级超级超级超级长的文本";
        label.numberOfLines = 0;
        label.textColor = [UIColor blackColor];
        [label sizeToFit];
        
        _messageLabel = label;
        return _messageLabel;
    }
    return _messageLabel;
}

- (UIButton *)registerButton
{
    if (_registerButton == nil) {
        UIButton *button = [[UIButton alloc] init];
        [button setTitle:@"注册" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        
        // 添加点击事件
        [button addTarget:self action:@selector(registClick) forControlEvents:UIControlEventTouchUpInside];
        
        _registerButton = button;
        return _registerButton;
    }
    return _registerButton;
}

- (UIButton *)loginButton
{
    if (_loginButton == nil) {
        UIButton *button = [[UIButton alloc] init];
        [button setTitle:@"登陆" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
        
        _loginButton = button;
        return _loginButton;
    }
    return _loginButton;
}

- (UIImageView *)coverView
{
    if (_coverView == nil ) {
        UIImageView *imageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
        _coverView = imageView;
        return _coverView;
    }
    
    return _coverView;
}


@end
